package dl.bukkit.myststus.vexshow;

import dl.bukkit.myststus.MyStatus;
import dl.bukkit.myststus.configuration.ConfigurationManager;
import dl.bukkit.myststus.library.ats.GVexHud;
import dl.bukkit.myststus.loader.Loader;
import dl.bukkit.myststus.logger.Logger;
import dl.bukkit.myststus.vexshow.sub.Image;
import dl.bukkit.myststus.vexshow.sub.Text;
import dl.bukkit.myststus.vexshow.sub.dynamic.ScaleHud;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

public class VexHudManager implements Loader {

    private static VexHudManager inst;

    private final MyStatus plugin;

    private final List<Class<? extends AbstractVexHud>> list = new LinkedList<>();
    private final Map<Class<? extends AbstractVexHud>, Map<String, AbstractVexHud>> map = new HashMap<>();

    private VexHudManager(MyStatus plugin) {
        inst = this;
        this.plugin = plugin;
    }

    @Override
    public void onLoad() {
        register(Image.class);
        register(ScaleHud.class);
        register(Text.class);
    }

    @Override
    public void staring() {
        list.forEach(this::load);
        Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, () -> {
                    for (Map.Entry<Class<? extends AbstractVexHud>, Map<String, AbstractVexHud>> entry : map.entrySet()) {
                        Logger.debug("§a===>> §3文件夹§6[§a", entry.getKey().getAnnotation(GVexHud.class).folder(), "§6] §a<<===");
                        entry.getValue().values().forEach(AbstractVexHud::loadTrigger);
                    }
        });
    }

    public static void register(Class<? extends AbstractVexHud> cls) {
        inst.list.add(cls);
    }

    private void load(Class<? extends AbstractVexHud> cls) {
        GVexHud annotation = cls.getAnnotation(GVexHud.class);
        if (annotation == null) {
            return;
        }
        File folder = ConfigurationManager.builderFile(annotation.folder().toLowerCase());
        if (! folder.exists()) {
            boolean mkdirs = folder.mkdirs();
            return;
        }
        try {
            Logger.debug("§a===>> §3开始载入", annotation.folder().toLowerCase(), "文件夹 §a<<===");
            Constructor<? extends AbstractVexHud> constructor = cls.getDeclaredConstructor(String.class, YamlConfiguration.class);
            constructor.setAccessible(true);
            int index = 0;
            for (File file : Objects.requireNonNull(folder.listFiles())) {
                AbstractVexHud vexHud = constructor.newInstance(file.getName(), YamlConfiguration.loadConfiguration(file));
                map.computeIfAbsent(vexHud.getClass(), t -> new HashMap<>()).put(vexHud.getKey().toLowerCase(), vexHud);
                index++;
            }
        } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
